#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
sys.path.insert(0, '.')
import os


file_list_path = r'/rootfs/media/yery/Kaso/data/VideoGenderCropPerson/BoxFeatureMerge.norm.txt'
out_file_list_path = r'/rootfs/media/yery/Kaso/data/VideoGenderCropPerson/FaceIDBoxFeatureMerge.norm.txt'


def main():
    face_id = 0
    video_faceids = {}
    video_image_counts = {}
    file_names = []
    with open(file_list_path, 'r') as file:
        for line in file.readlines():
            # gd/zs/dqjd/xgly/150100414a5444345203bcd04294b500/video/20190912_154308/00010_000.jpg 154,69,290,245,1,1024
            file_name = line.strip().split()[0]
            video_name = os.path.dirname(file_name)
            cur_face_id = video_faceids.get(video_name, None)
            if cur_face_id is None:
                face_id += 1
                video_faceids[video_name] = face_id
                video_image_counts[video_name] = 1
            else:
                video_image_counts[video_name] += 1
            file_names.append(file_name)

    with open(out_file_list_path, 'w') as file:
        for file_name in file_names:
            video_name = os.path.dirname(file_name)
            face_id = video_faceids[video_name]
            image_count = video_image_counts[video_name]
            out_info = '{} {} {} {}\n'.format(face_id, face_id, file_name, image_count)
            file.write(out_info)
    os.system('chmod a+wr {}'.format(out_file_list_path))
    print('Finish!')


if __name__ == '__main__':
    main()
